package com.example.demo20230927;

import com.google.protobuf.Message;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
@RequestMapping("/chat")
public class ChatController {

    private final RestTemplate restTemplate;

    @Autowired
    public ChatController(RestTemplate restTemplate) {
        this.restTemplate = restTemplate;
    }

    @PostMapping("/send")
    public String sendMessage(@RequestBody Message message) {
        // 这里调用OpenAI的API
        String openAiUrl = "https://api.openai.com/v4/...";
        // 假设使用RestTemplate调用OpenAI API
        String response = restTemplate.postForObject(openAiUrl, message, String.class);
        return response; // 将OpenAI的回复直接返回给客户端
    }
}